package com.foreverwin.mes.core.meext;

import com.foreverwin.mes.core.utils.ExceptionUtils;
import com.sap.me.globalization.ErrorMessageResourcesGlobalizationService;
import com.visiprise.frame.service.ServiceActivationException;
import com.visiprise.frame.service.XmlConfigurationLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import javax.annotation.PostConstruct;

/**
 * @author Ervin Chen
 * @date 2020/2/25 15:32
 */
@Profile({"dev", "qas", "prod"})
@Configuration
public class ServiceLoader {
    private final Logger logger = LoggerFactory.getLogger(ServiceLoader.class);

    private static String[] SERVICE_CONFIG_URI_LIST = {
            "service-config.xml",
            "hook/service-config.xml",
            "service/service-config.xml",
            "serviceext/service-config.xml"
    };

    @PostConstruct
    public void loadService() {
        logger.info("MES扩展服务加载");
        ClassLoader classLoader = this.getClass().getClassLoader();
        for (String uri : SERVICE_CONFIG_URI_LIST) {
            XmlConfigurationLoader xmlConfigurationLoader = new XmlConfigurationLoader();
            try {
                xmlConfigurationLoader.loadConfiguration(uri, classLoader);
            } catch (ServiceActivationException e) {
                logger.error(ExceptionUtils.toString(e));
            }
            /*try {
                Enumeration<URL> url = classLoader.getResources(uri);
                while (url.hasMoreElements()) {
                    System.out.println(url.nextElement().getPath());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }*/
        }
        ErrorMessageResourcesGlobalizationService.bundleControl.addExtensionBundle("meext.ErrorMessages", classLoader != null ? classLoader : this.getClass().getClassLoader());
    }

}
